package com.qingfeng.music;

import com.qingfeng.music.dao.DaoHelper;
import com.qingfeng.music.dao.Music;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地数据库歌曲管理
 *
 * Created by Ganlin.Wu on 2016/9/25.
 */
public class MusicManager {

    private List<Music> localMusicList; //本地歌曲列表

    //---------------------------------------
    //单例模式
    private static MusicManager instance;
    private MusicManager() {
    }
    public static MusicManager getInstance() {
        if (instance == null) {
            synchronized (MusicManager.class) {
                if (instance == null) {
                    instance = new MusicManager();
                }
            }
        }
        return instance;
    }


    //---------------------------------------------------

    /**
     * 加载本地数据库音乐列表到内存中（MusicDao）
     *
     */
    public synchronized void loadLocalMusicList() {
        if (localMusicList == null) {
            localMusicList = new ArrayList<>();
        }
        localMusicList.clear();
        localMusicList = DaoHelper.getInstance().getMusicDao().loadAll();

    }

    /**
     * 加载内存中的歌曲列表
     *
     * @return
     */
    public List<Music> getLocalMusicList() {
        return localMusicList == null ? new ArrayList<Music>() : localMusicList;
    }

    /**
     * 取得单个歌曲
     *
     * @param id
     * @return
     */
    public Music getMusicById(long id) {
        for (Music music : localMusicList) {
            if (music.getId() == id) {
                return music;
            }
        }
        return null;
    }

}
